package com.wanqing.community.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wanqing.community.bean.UserBean;
import com.wanqing.community.dao.UserLoginDao;
import com.wanqing.community.manager.UserLoginManager;
import com.wanqing.community.util.CommunityException;
import com.wanqing.community.util.ConditionParse;
import com.wanqing.community.util.MD5;

/**
 * 用户manager层实现类
 *  
 * Copyright 2014 
 * @version 
 * Company
 */
@Service("userLoginManager")
public class UserLoginManagerImpl implements UserLoginManager {
	
	private static Logger log = Logger.getLogger(UserLoginManagerImpl.class);
	
	@Resource
	private UserLoginDao  userLoginDao;
	
	/**
	 *<p>Description:添加用户<p>
	 * <p>modifiContent:</p>
	 * @param list
	 * @return boolean
	 */
	public boolean addUserLogin(UserBean user) throws CommunityException{
		int total = 0;
		try {
			total =  userLoginDao.addUserLogin(user);			
		} catch (CommunityException e) {
			log.error("添加用户时出现异常信息!"+e.toString());
			throw e;
		}
		return total>0?true:false;
	}
	
	/**
	 * 
	 *<p>Description:删除用户<p>
	 * <p>modifiContent:</p>
	 * @param list
	 * @return
	 */
	public boolean deleteUserLogin(List<String> list) {
		try {
			int count = userLoginDao.deleteUserLogin(list);
			if(count >0){
				return true;
			}
		} catch (CommunityException e) {
			 log.error("删除用户时出现异常信息！");
		}
		return false;
	}
 	 /**
 	  * 
 	  *<p>Description:修改用户<p>
 	  * <p>modifiContent:</p>
 	  * @param user
 	  * @return
 	  */
	public boolean updateUserLogin(UserBean user) {
		int count = 0;
		try {
			 count = userLoginDao.updateUserLogin(user);
			
		} catch (CommunityException e) {
			log.error("修改用户时出现异常信息！");
		}
		return count > 0 ? true :false;
	}

	/**
	  * 
	  * <p>Description:用户登录<p>
	  * <p>modifiContent:<p>
	  * @param loginName
	  * @param loginPwd
	  * @return
	  * @throws CommunityException
	  */
	public UserBean userLogin(String loginName, String loginPwd)
			throws CommunityException {
		UserBean user = null;
		try {
			user = this.userLoginDao.userLoign(loginName, MD5.md5Encryption(loginPwd));			
		} catch (CommunityException e) {
			log.error("用户登录异常！"+e.toString());
			throw e;
		}
		return user;
	}
	
	public int getUserCount(ConditionParse condParse,boolean isAll) throws CommunityException {		
		int total = 0;
		try {
			if(isAll){
				total = this.userLoginDao.getUserCounts(condParse);
			}else{
				total = userLoginDao.getUserCount(condParse);
			}						
		} catch (CommunityException e) {
			log.error("得到用户信息"+e.toString());
			throw e;
		}
		return total;
	}
	
	public List<UserBean> queryUser(ConditionParse condParse,
			int currPage, int pageSize) throws CommunityException {		
		List<UserBean> list = null;
		try {
			 list = userLoginDao.queryUser(condParse, currPage, pageSize);			
		} catch (CommunityException e) {
			log.error("分页查询用户信息"+e.toString());
			throw e;
		}
		return list;
	}
	
	/**
	  * <p>Description:根据ID查找用户<p>
	  * <p>modifiContent:<p>
	  * @param id
	  * @return
	  * @throws CommunityException
	  */
	public UserBean findUserById(String id) throws CommunityException {		
		UserBean user = new UserBean();
		try {
			user = this.userLoginDao.findUserById(id);
		} catch (CommunityException e) {
			log.error("根据ID查找用户异常！"+e.toString());
			throw e;
		}
		return user;
	}
	
	/**
	  * <p>Description:根据loginName查找用户<p>
	  * <p>modifiContent:<p>
	  * @param loginName
	  * @return
	  * @throws CommunityException
	  */
	@Override
	public UserBean findUserByEmail(String email) throws CommunityException {		
		UserBean user = new UserBean();
		try {
			user = this.userLoginDao.findUserByEmail(email);
		} catch (CommunityException e) {
			log.error("根据ID查找用户异常！"+e.toString());
			throw e;
		}
		return user;
	}
	
	public boolean updatePwd(UserBean userLogin) throws CommunityException {
		int count = 0;
		try {
			 count = userLoginDao.updatePwd(userLogin);		
		} catch (CommunityException e) {
			log.error("修改密码出现异常信息！");
		}
		return count > 0 ? true :false;
	}
	
	public boolean addUserLogin(List<UserBean> list) throws CommunityException {
		int count = 0;
		try {
			 count = userLoginDao.addUserLogin(list);
		} catch (CommunityException e) {
			log.error(e.toString());
		}
		return count > 0 ? true :false;
	}
	
	public List<UserBean> listUser() throws CommunityException {
		List<UserBean> list = null;
		try{
			list = this.userLoginDao.listUser();
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}
	
	/**
	 * <p>Description:查询用户参照总数<p>
	 * @author WJX
	 * @version 2014-11-24
	 * @modifier WJX
	 * @modifiDate 2014-11-24
	 * <p>modifiContent:<p>
	 */
	public int getRefUserCount(Map<String, Object> queryMap)throws CommunityException{
		int total = 0;
		try{
			total = this.userLoginDao.getRefUserCount(queryMap);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return total;
	}
	
	/**
	 * <p>Description:查询用户参照列表<p>
	 * @author WJX
	 * @version 2014-11-24
	 * @modifier WJX
	 * @modifiDate 2014-11-24
	 * <p>modifiContent:<p>
	 */
	public List<UserBean> listRefUser(Map<String, Object> queryMap,int currPage,
			int pageSize)throws CommunityException{
		List<UserBean> list = null;
		try{
			list = this.userLoginDao.listRefUser(queryMap, currPage, pageSize);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}

}
